package com.order.controller.sys;

import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.order.config.Consts;
import com.order.model.Account;
import com.order.shiro.ShiroUtils;

/**
 * @描述：客户控制器，包括查找、更新、保存等操作
 * @作者： linbo
 */
@ControllerBind(controllerKey = Consts.SYS + "account")
public class AccountController extends Controller {


	private String message = "";
	private Account account = new Account();
	

	/** 客户主页	 */
	public void index() {
		render(Consts.SYS + "account.htm");
	}

	
	/**更新 */
	public void toUpdate() {
		account.getObj(getPara("json"));
		if (account.myUpdate()) 
			message = "修改成功！";
		setAttr("msg", "温馨提示："+message);
		renderJson();
	}


	/** 修改密码，先验证旧密码，再update新密码*/
	public void password(){		
		String json = getPara("json");
		JSONObject jsonObj = JSONObject.parseObject(json);// 将字符串转化成json对象
		String password = jsonObj.getString("password");// 获取字符串。
		String newpassword = jsonObj.getString("newpassword");
		String repassword = jsonObj.getString("repassword");
		
		if ("".equals(newpassword) || "".equals(repassword) || (! newpassword.equals(repassword))){
			message ="两次输入的密码不一致！";
		} else if("".equals(password) || !account.validatorpwd(password, ShiroUtils.getUserId())){
			message ="旧密码错误！";
		}else{
			account.password(newpassword, ShiroUtils.getUserId());
			message ="修改成功！";
		}
		System.out.println(message);
		setAttr("msg","温馨提示："+ message);
		renderJson();
	}

	/** 客户查询方法，如果没有条件则查询所有*/
	public void getAccountJson(){
		String jsonStr = "{\"NAME\":\"\",\"STATUS\":\"\",\"VIP\":\"\",\"ADDRESS\":\"\",\"CREATEDATE\":\"\",\"GENDER\":\"\"}";
		int pagesize = (int) getParaToInt("pagesize");
		int page = (int) getParaToInt("page");
		JSONObject jsonObj = null;
		Map<String, Object> map = null;
		String condition = getPara("condition","");
		System.out.println("condition:"+condition);
		String wherejson = condition.replace("[", "").replace("]", "");		
		if (StringKit.notBlank(wherejson)) {			
			jsonObj = JSONObject.parseObject(wherejson);
			String field = jsonObj.getString("field");
			String value = jsonObj.getString("value");
			jsonStr = "{\"" + field + "\":\"" + value + "\",\"EMAIL\":\"\"}";
			jsonObj = JSONObject.parseObject(jsonStr);
		}else{
			jsonObj = JSONObject.parseObject(getPara("where", jsonStr));			
		}
		map = Account.dao.getAccountJson(page, pagesize, jsonObj);
		renderJson(map);
	}
		
}
